package com.visikard.request;

import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

import com.visikard.application.APIURL;
import com.visikard.application.Constant;
import com.visikard.application.VKregisterProperties;
import com.visikard.net.VKregisterException;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.net.VkRequestMethod;
import com.visikard.net.VkRestClient;
import com.visikard.system.VKregisterConnects;
import com.visikard.utility.HttpsUtils;

public class AccountRequest extends BaseRequest {

	/**
	 * constructor
	 */
	public AccountRequest() {
		super();
	}

	/**
	 * login app reqest
	 * @param username
	 * @param password
	 * @return
	 */
	public String login(String email, String password, String isMerchant, String merchantCode, String storeCode)
			throws VKregisterException {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp() + APIURL.VK_REGISTER_LOGIN);
		rc.addParam("email", email);
		rc.addParam("password", password);
		rc.addParam("isMerchant", isMerchant);
		rc.addParam("merchantCode", merchantCode);
		rc.addParam("storeCode", storeCode);
		// rc.addParam("deviceToken", GCMIntentService.regId);
		rc.addParam("app", Constant.ANDROID_ID_NUM);
		// rc.addParam("keyStore", VKregisterProperties.SERVER_KEYSTORE);
		rc.execute(VkRequestMethod.POST);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			Log.e(Constant.LOG_TAG, "login " + rc.getErrorMessage());
			return rc.getErrorMessage();
		}
		return rc.getResponse();
	}

	// forgot pass
	public String sendEmailVK(String email) throws VKregisterException {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttps() + APIURL.VK_ACCOUNT_FORGOT_PASS);
		rc.addParam("email", email);
		rc.addParam("keyStore", VKregisterProperties.SERVER_KEYSTORE);

		rc.execute(VkRequestMethod.POST);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			Log.e(Constant.LOG_TAG, "sendEmailVK " + rc.getErrorMessage());
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	public void logout(long uid, String loginSession) throws VKregisterException {
		try {
			HttpClient httpclient = HttpsUtils.getInstance().sslClient(new DefaultHttpClient());
			String httpurl = VKregisterConnects.getApiContextPathHttp() + APIURL.VK_REGISTER_LOGOUT;
			HttpPost httppost = new HttpPost(httpurl);

			HttpParams timeoutParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(timeoutParams, Constant.DEFAULT_TIMEOUT);
			HttpConnectionParams.setSoTimeout(timeoutParams, Constant.DEFAULT_TIMEOUT);
			httppost.setParams(timeoutParams);

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("fkUser", String.valueOf(uid)));
			nvps.add(new BasicNameValuePair("sessionid", loginSession));
			// nvps.add(new BasicNameValuePair("deviceToken", GCMRegId));
			nvps.add(new BasicNameValuePair("app", Constant.ANDROID_ID_NUM));

			httppost.setEntity(new UrlEncodedFormEntity(nvps));
			httpclient.execute(httppost);
		}
		catch (ConnectException e) {
			throw new VKregisterNetworkException(e.getMessage());
		}
		catch (Exception e) {
			throw new VKregisterException(e.getMessage());
		}
	}

	// staff request access
	public String staffRequestAccess(String fname, String lname, String email, String merchantCode, String storeCode)
			throws VKregisterException {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp() + APIURL.STAFF_REQUEST_ACCESS);
		rc.addParam("fname", fname);
		rc.addParam("lname", lname);
		rc.addParam("email", email);
		rc.addParam("merchantCode", merchantCode);
		rc.addParam("storeCode", storeCode);

		rc.execute(VkRequestMethod.POST);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			Log.e(Constant.LOG_TAG, "sendEmailVK " + rc.getErrorMessage());
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	// staff request access
	public String merchantGetDashBoardInfo(long merchantId, long idKards) throws VKregisterException {
		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp() + APIURL.MERCHANT_GET_DASHBOARD);
		rc.addParam("merchantId", merchantId);
		rc.addParam("idKards", idKards);

		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			Log.e(Constant.LOG_TAG, "sendEmailVK " + rc.getErrorMessage());
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}
}